﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Stock
// Project Description :    
// ===============================================================================
// Class Name          :    AdminLogic
// Namespace:          :    Gfw.BLL.Basic.Stock
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/7/14 15:07:29
// Update Time         :    2012/7/14 15:07:29
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.BLL.Basic.Stock.Facilities;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Infrastructure.Facilities;
using Gfw.Infrastructure.Stock.Enums;
using Gfw.Infrastructure.Stock.Facilities;
using Gfw.Models.Stock;
using Gfw.Repositories.Stock;
using Gfw.Repository;
using Gfw.Web.Models.Stock;

namespace Gfw.BLL.Basic.Stock
{
    /// <summary>
    /// 管理股票逻辑
    /// </summary>
    public static class UsAdminLogic
    {
        #region 获取股票价格
        public static decimal GetMarketPrice()
        {
            MarketRepository rep = new MarketRepository();
            var market = rep.Unique();
            return market.CurrentPrice;
        }
        #endregion

        #region 股票管理帐户
        public static AdminAccountModel GetAccount()
        {
      
            AdminAccountRepository rep = new AdminAccountRepository();
            StkAdminaccount account = rep.Unique();
            return account.CopyTo<AdminAccountModel>();
        }


        #endregion

        #region 创建股票 & 记录
        /// <summary>
        /// 查询股票发行记录
        /// </summary>
        /// <returns></returns>
        public static IList<CreateHistroyModel> GetCreateHistories()
        {
            WholesaleRepository rep = new WholesaleRepository();
            var items = rep.Query();
            var result = new List<CreateHistroyModel>();
            foreach (var item in items)
            {
                result.Add(item.CopyTo<CreateHistroyModel>());
            }
            return result;
        }

        /// <summary>
        /// 增加股票发行记录
        /// </summary>
        public static void Create(CreateHistroyModel model)
        {

            model.WholesalePrice = GetMarketPrice();

            UnitOfWork work = new UnitOfWork();
            WholesaleRepository wholeRep = new WholesaleRepository();
            var history = model.CopyTo<StkWholesale>();

            wholeRep.Save(history, work);

            AdminAccountRepository adminRep = new AdminAccountRepository();
            StkAdminaccount account = adminRep.Unique(work);
            account.StockCount += model.WholesaleCount;
            if (model.WholesaleType == (int)StockPublicType.Original)
            {
                account.OriginalStockMoney += model.WholesaleCount;
            }
            adminRep.Update(account, work);

            work.Commit();
        }

        #endregion

        #region 股票交易

        #region 卖出股票
        /// <summary>
        /// 获取股票交易的实体
        /// </summary>
        /// <returns></returns>
        public static SellModel GetSellModel()
        {
            AdminAccountRepository accountRep = new AdminAccountRepository();
            var account = accountRep.Unique();

            var price = StockManager.CurrentPrice;
            SellModel model = new SellModel()
                                  {
                                      CurrentPrice = price,
                                      MaxCount = account.StockCount
                                  };
            return model;
        }

        public static void Sell(SellModel model, string userId)
        {

            AdminSellTradeFacilityImpl facility = new AdminSellTradeFacilityImpl(model.SellCount, model.CurrentPrice);
            facility.Execute();
        }
        #endregion

        #region 买入股票
        public static BuyModel GetBuyModel()
        {
            AdminAccountRepository accountRep = new AdminAccountRepository();
            var account = accountRep.Unique();

            var price = StockManager.CurrentPrice;
            BuyModel model = new BuyModel()
            {
                Balance = account.StockMoney,
                BuyCount = 0,
                CurrentPrice = price,
                MaxCount = Convert.ToInt32((account.StockMoney / (price * 100))) * 100
            };
            return model;
        }

        public static void Buy(BuyModel model,string userId)
        {
            AdminBuyTradeFacilityImpl facility = new AdminBuyTradeFacilityImpl(model.BuyCount, model.CurrentPrice);
            facility.Execute();
        }
        #endregion

        #endregion

        #region 股票市场管理
        public static MarketRunningStateEnum GetMarketRunnintState()
        {
           var state = StockManager.GetMarketState();
           return (MarketRunningStateEnum)Enum.ToObject(typeof(MarketRunningStateEnum), state);
        }

        public static void SetMarketRunningState(MarketRunningStateEnum state)
        {
            if (!StockManager.SetMarketState((int)state))
            {
                throw new Exception("游戏服务未运行。");
            }
        }
        #endregion


        #region 股票拆分
        public static void MarketSplit(int multiple)
        {
            StockSplitFacility facility = new StockSplitFacility(multiple);
            facility.Execute();
        }

        #endregion
    }
}
